#include "Tutorial1Level.h"
#include "GameplayBackground.h"
#include "il_assert.h"

Tutorial1Level::~Tutorial1Level()
{
}

void Tutorial1Level::Load( Dock* docks, GameplayBackground* background ) 
{
	background->Load( "level1_shore_bg_top", "level1_shore_bg_bottom" );   
	
	// setup the docks
	docks[0].Load( CIwFVec2(229, 39), CIwFVec2(1,0), DockType::RED );
	
	// setup the second dock to be outside the screen
	docks[1].Load( CIwFVec2(-800, -800), CIwFVec2(-1,0), DockType::YELLOW );
	
	// load the overlays
	m_tap_and_drag_overlay.Load( "tap_and_drag_overlay" );
	m_unloading_overlay.Load( "unloading_overlay" );
	m_leave_screen_overlay.Load( "leave_screen_overlay" );
	
	m_current_state = TutorialLevelState::START;
	m_ship = NULL;
}

void Tutorial1Level::Unload()
{
	m_tap_and_drag_overlay.Unload();
	m_unloading_overlay.Unload();
	m_leave_screen_overlay.Unload();
}

// return -1 todo nothing
// return 1 to start a new ship with the provided data
// return 2 to show an overlay message
// return 3 if the level is over
LevelUpdateReturnStatus::Enum Tutorial1Level::Update( DockType::Enum *type, CIwFVec2* p1, CIwFVec2* p2, float dt )
{
	if( m_current_state == TutorialLevelState::START ) {
		// start the "tutorial boat"
		p1->x = 520;
		p1->y = 200;
		
		p2->x = 480;
		p2->y = 200;
		
		*type = DockType::RED;
		
		m_current_state = TutorialLevelState::WAITING_FOR_SHIP;
		
		return LevelUpdateReturnStatus::START_SHIP;
	}
	
	if( m_current_state == TutorialLevelState::WAITING_FOR_SHIP ) {
		// wait until the ship visible on screen
		if( m_ship->GetPos().x <= 460 ) {
			m_current_state = TutorialLevelState::TAP_AND_DRAG_OVERLAY;
		}
	}
	
	if( m_current_state == TutorialLevelState::TAP_AND_DRAG_OVERLAY ) {
	
		if( m_tap_and_drag_overlay.IsContinuePressed() == true ) {
			m_current_state = TutorialLevelState::WAITING_FOR_DOCKING;
		}
	
		return LevelUpdateReturnStatus::SHOW_OVERLAY;
	}
	
	if( m_current_state == TutorialLevelState::WAITING_FOR_DOCKING ) {
		if( m_ship->IsDocking() == true ) {
			// the ship started docking
			m_current_state = TutorialLevelState::UNLOADING_OVERLAY;
		}
	}
	
	if( m_current_state == TutorialLevelState::UNLOADING_OVERLAY ) {
		if( m_unloading_overlay.IsContinuePressed() == true ) {
			m_current_state = TutorialLevelState::WATING_FOR_UNLOAD;
		}
		
		return LevelUpdateReturnStatus::SHOW_OVERLAY;
	}
	
	if( m_current_state== TutorialLevelState::WATING_FOR_UNLOAD ) {
		if( m_ship->IsDocking() == false ) {
			// the ship is done unloading
			m_current_state = TutorialLevelState::LEAVE_SCREEN_OVERLAY;
		}
	}
	
	if( m_current_state == TutorialLevelState::LEAVE_SCREEN_OVERLAY ) {
		if( m_leave_screen_overlay.IsContinuePressed() == true ) {
			m_current_state = TutorialLevelState::WATING_FOR_LEAVE_SCREEN;
		}
		
		return LevelUpdateReturnStatus::SHOW_OVERLAY;
	}
	
	if( m_current_state == TutorialLevelState::WATING_FOR_LEAVE_SCREEN ) {
		
		if( m_ship->OutsideScreen() == true ) {
			return LevelUpdateReturnStatus::LEVEL_COMPLETE;
		}
	}

	return LevelUpdateReturnStatus::DO_NOTHING;
}

void Tutorial1Level::ShipAdded( Ship *ship ) 
{
	IL_ASSERT( m_ship == NULL, "multiple ships were added, this is not planned ... ");
	m_ship = ship;
}

void Tutorial1Level::Draw ()
{
	if( m_current_state == TutorialLevelState::TAP_AND_DRAG_OVERLAY ) {
		m_tap_and_drag_overlay.Draw();
	}
	
	if( m_current_state == TutorialLevelState::UNLOADING_OVERLAY ) {
		m_unloading_overlay.Draw();
	}
	
	if( m_current_state == TutorialLevelState::LEAVE_SCREEN_OVERLAY ) {
		m_leave_screen_overlay.Draw();
	}
}